<?php
/**
 * Created by PhpStorm.
 * User: qinlu
 * Date: 2018/8/04
 * Time: 13:57
 */
namespace Api\Controller;

//限时秒杀管理控制器
class SecKillController extends BaseController 
{

	//秒杀时间
	public function killTime(){
		$nowTime = time();
		// $kill_time = M('kill')->field('kill_time')->select();
		// foreach ($kill_time as $key => $value) {
  //           $time[$key]['time']  = $value['kill_time'];
  //           $time[$key]['start_time'] = $value['start_time'];
  //           $time[$key]['end_time'] = $value['end_time'];
		// }
        $time = $this->flash_sale_time_space();
		$this->returnInfo('1','获取成功',$time);
	}

	//秒杀时间对应的商品列表
    public function killList()
    {
        $start_time = time();
        $end_time = I('end_time');
        $list = M('flash_sale as f')
           ->join('ln_goods as g on g.goods_id=f.goods_id')
           ->join('ln_kill as k on k.kill_id=f.kill_id')
           ->field('k.kill_time,f.kill_id,f.goods_id,g.goods_name,g.market_price,f.price,g.shop_price,g.original_img,g.prom_type,g.shop_id,f.start_time,f.end_time')
           ->where([
               "f.start_time" => ["lt",time()],
               "f.end_time" => ["gt",$end_time],
               "f.is_end" => 0,
           ])
           ->select();
        foreach($list as $k => &$v)
        {
            $v['original_img'] = parseMediaUrl($v['original_img']);
        }
        $list = $list?$list:[];
		$this->returnInfo('1','获取成功',$list);

	}


    function flash_sale_time_space()
    {
    $now_day = date('Y-m-d');
    $now_time = date('H');
    if ($now_time % 2 == 0) {
        $flash_now_time = $now_time;
    } else {
        $flash_now_time = $now_time - 1;
    }
    $flash_sale_time = strtotime($now_day . " " . $flash_now_time . ":00:00");
    $space = 7200;
    $time_space = array(
        array('time' => date("H:i", $flash_sale_time), 'start_time' => $flash_sale_time, 'end_time' => $flash_sale_time + $space),
        array('time' => date("H:i", $flash_sale_time + $space), 'start_time' => $flash_sale_time + $space, 'end_time' => $flash_sale_time + 2 * $space),
        array('time' => date("H:i", $flash_sale_time + 2 * $space), 'start_time' => $flash_sale_time + 2 * $space, 'end_time' => $flash_sale_time + 3 * $space),
        array('time' => date("H:i", $flash_sale_time + 3 * $space), 'start_time' => $flash_sale_time + 3 * $space, 'end_time' => $flash_sale_time + 4 * $space),
        array('time' => date("H:i", $flash_sale_time + 4 * $space), 'start_time' => $flash_sale_time + 4 * $space, 'end_time' => $flash_sale_time + 5 * $space),
    );
    return $time_space;
}


	/**
     * 商品详情页
     */
    public function kill_goods()
    {
        try{
            $goodsLogic = new \Api\Logic\GoodsLogic();
            $goods_id = I("goods_id");
            $map['goods_id'] = $goods_id;
            $map['is_end'] = 0;
            $kill_goods_info = M('flash_sale')->where($map)->find(); // 找出这个商品
            if(!$kill_goods_info)
            {
                $this->returnInfo('-1','商品不存在');
            }
            $where['goods_id'] = $goods_id;
            $goods = M('Goods')->field('goods_id,prom_type,goods_name,market_price,shop_price,goods_remark,original_img,shop_id')->where($where)->find();
            $goods['original_img'] = parseMediaUrl($goods['original_img']);
            $goods['shop_price'] = $kill_goods_info['price'];
            $goods_images_list = M('GoodsImages')->field('image_url')->where("goods_id = $goods_id")->select(); // 商品 图册
            foreach($goods_images_list as $key => &$goodsval)
            {
               $goodsval['image_url'] = parseMediaUrl($goodsval['image_url']);
            }
            $goods_attribute = M('GoodsAttribute')->getField('attr_id,attr_name'); // 查询属性
            $goods_attr_list = M('GoodsAttr')->where("goods_id = $goods_id")->select(); // 查询商品属性表

            $Model = new \Think\Model();
            // 商品规格 价钱 库存表 找出 所有 规格项id
            $keys = M('SpecGoodsPrice')->where("goods_id = $goods_id")->getField("GROUP_CONCAT(`key` SEPARATOR '_') ");
            if($keys)
            {
                $specImage =  M('SpecImage')->where("goods_id = $goods_id and src != '' ")->getField("spec_image_id,src");// 规格对应的 图片表， 例如颜色
                $keys = str_replace('_',',',$keys);
                $sql  = "SELECT a.name,a.order,b.id,b.* FROM ln_spec AS a INNER JOIN ln_spec_item AS b ON a.id = b.spec_id WHERE b.id IN($keys) ORDER BY b.spec_id, b.id";
                 $filter_spec2 = $Model->query($sql);
                foreach($filter_spec2 as $val){
                if(!array_key_exists($val['name'])){
                    $filter_spec[$val['name']]['spec_name'] = $val['name'];
                }
                $filter_spec[$val['name']]['values'][] = [
                    'item'=> $val['item'],
                    'item_id'=> $val['id'],
                    'src' =>$specImage[$val['id']] ? parseMediaUrl($specImage[$val['id']]) : ""
                ];
            }

                 $goods['goods_spec_list'] = array_values($filter_spec);
            }
            $spec_goods_price  = M('spec_goods_price')->where("goods_id = $goods_id")->getField("key,price,store_count"); // 规格 对应 价格 库存表
            M('Goods')->where("goods_id=$goods_id")->save(array('click_count'=>$goods['click_count']+1 )); // 统计点击数
            $commentStatistics = $goodsLogic->commentStatistics($goods_id);// 获取某个商品的评论统计
            $navigate_goods = navigate_goods($goods_id,1); // 面包屑导航
            // 输出客服
            $shop_kefu = ''; 
            $shop = M('shop')->where([
                "id" => $goods['shop_id']
            ])->field('id as shop_id,user_id')->find();
            if( $shop )
            $shop_kefu = $shop['user_id'];
            $result = array(
                'shop_kefu'=>$shop_kefu,
                'kill_goods_info'=>$kill_goods_info,
                'spec_goods_price'=>$spec_goods_price,
                // 'navigate_goods'=>$navigate_goods,
                'commentStatistics'=>$commentStatistics,
                'goods_attribute'=>$goods_attribute,
                'goods_attr_list'=>$goods_attr_list,
                'filter_spec'=>$filter_spec?$filter_spec:[],
                'goods_images_list'=>$goods_images_list,
                'goods'=>$goods,
            );
            $this->returnInfo('1','获取成功',$result);
        }catch(\Exception $e)
        {
            $this->returnInfo('-1','获取失败', $e->getMessage() );
        }
     }




    




}